package com.rightsidetech.mall.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class MallPubsubConfiguration {

    @Bean
    public RedisTemplate<String,String> redisTemplate(@Autowired RedisConnectionFactory factory){
        RedisTemplate<String,String> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
//        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
//        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(@Autowired RedisConnectionFactory redisConnectionFactory,
                                                                       MessageListenerAdapter priceListenerAdapter){
        RedisMessageListenerContainer messageListener = new RedisMessageListenerContainer();
        messageListener.setConnectionFactory(redisConnectionFactory);
        messageListener.addMessageListener(priceListenerAdapter,new PatternTopic("MALL_LOG"));
        return messageListener;
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapter(@Autowired MallReceiverRedisMessage mallReceiverRedisMessage){
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(mallReceiverRedisMessage,"mallLogReceiver");
        return  messageListenerAdapter;
    }

}
